package com.project.cotroller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("cookie")
public class CookieController {
    @RequestMapping("getCookie")
    public String getCookie(HttpServletResponse response){
        //创建cookie对象，并添加cookie数据
        Cookie cookie = new Cookie("myName","123");
        cookie.setMaxAge(20000);
        //将cookie信息发送给客户端
        response.addCookie(cookie);
        return "ok";
    }

    @RequestMapping("printCookie")
    public String printCookie(HttpServletRequest request){
        Cookie[] cookieArray = request.getCookies();
        String str = "cookie信息:";
        for (Cookie c:cookieArray){
            str += c.getName()+"   "+c.getValue()+"<br>";
        }
        return  str;
    }
}
